home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Toolbox / Tabs LDEF 1.0 / Sources / Windows.c < prev   
Encoding:
C/C++ Source or Header  |  1996-01-16  |  8.3 KB  |  365 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Windows.c
  3.  
  4.     Contains:    Handle application's windows
  5.  
  6.     Written by:    Chris White, Developer Technical Support
  7.     
  8.     Copyright:    Â© 1995 by Apple Computer, Inc., all rights reserved.
  9.     
  10.     Change History (most recent first):
  11.     
  12.             12/18/95            CW        First release
  13.  
  14. */
  15.  
  16.  
  17. #pragma segment Core
  18.  
  19.  
  20.  
  21. // System Includes
  22.  
  23. #ifndef __TYPES__
  24.     #include <Types.h>
  25. #endif
  26.  
  27. #ifndef __WINDOWS__
  28.     #include <Windows.h>
  29. #endif
  30.  
  31. #ifndef __QUICKDRAW__
  32.     #include <Quickdraw.h>
  33. #endif
  34.  
  35. #ifndef __RESOURCES__
  36.     #include <Resources.h>
  37. #endif
  38.  
  39. #ifndef __FONTS__
  40.     #include <Fonts.h>
  41. #endif
  42.  
  43. #ifndef __ERRORS__
  44.     #include <Errors.h>
  45. #endif
  46.  
  47.  
  48.  
  49.  
  50. // Application Includes
  51.  
  52. #ifndef __BAREBONES__
  53.     #include "BareBones.h"
  54. #endif
  55.  
  56. #ifndef __PROTOTYPES__
  57.     #include "Prototypes.h"
  58. #endif
  59.  
  60.  
  61.  
  62.  
  63. // Static prototypes
  64.  
  65. static OSErr    CreateContentList ( WindowRef theWindow, tContentsProcPtr contentsProc, void* refCon );
  66. static OSErr    AddListContents ( ListRef theList, void* refCon );
  67.  
  68.  
  69. OSErr            PatchListLDEF ( ListRef theList );
  70.  
  71.  
  72.  
  73.  
  74.  
  75. //
  76. // This is called to create the application's window.
  77. //
  78. void CreateWindow ( void )
  79. {
  80.     OSErr        theErr;
  81.     WindowRef    theWindow;
  82.     
  83.     theErr = CreateListWindow ( &theWindow, AddListContents, nil );
  84.     if ( theErr )
  85.         AlertUser ( kGenericErrorStr, theErr, "\p" );
  86.     
  87.     return;
  88. }
  89.  
  90.  
  91.  
  92. //
  93. // This will close the application's window, dispose of any storage we've hung
  94. // off the window, and then dispose of the window itself.
  95. //
  96. WindowRef DestroyWindow ( WindowRef windowRef )
  97. {
  98.     if ( windowRef )
  99.     {
  100.         ListRef theList;
  101.         
  102.         theList = (ListRef) GetWRefCon ( windowRef );
  103.         if ( theList )
  104.             LDispose ( theList );
  105.         
  106.         DisposeWindow ( windowRef );
  107.         
  108.     }
  109.     
  110.     return nil;
  111. }
  112.  
  113.  
  114.  
  115. //
  116. // Creates a window with a list in the centre. contentsProc is a routine
  117. // pointer to intitialize the list. It's passed the refCon to assist.
  118. //
  119. OSErr CreateListWindow ( WindowRef* windowRef, tContentsProcPtr contentsProc, void* refCon )
  120. {
  121.     OSErr        theErr;
  122.     WindowRef    theWindow;
  123.     
  124.     
  125.     theWindow = GetNewCWindow ( kDisplayWindow, nil, (WindowRef) -1 );
  126.     if ( theWindow == nil )
  127.         return (ResError ( )) ? ResError ( ) : resNotFound;
  128.     
  129.     SetPortWindowPort ( theWindow );
  130.     TextFont ( geneva );
  131.     TextFace ( normal );
  132.     TextSize ( 9 );
  133.     
  134.      theErr = CreateContentList ( theWindow, contentsProc, refCon );
  135.     if ( theErr )    goto CleanupAndBail;
  136.     
  137.     SelectWindow ( theWindow );
  138.     ShowWindow ( theWindow );
  139.     
  140.     *windowRef = theWindow;
  141.     
  142.     return noErr;
  143.     
  144.  
  145. CleanupAndBail:
  146.     
  147.     // Don't forget to free any storage we've used so far
  148.     DestroyWindow ( theWindow );
  149.     
  150.     return theErr;
  151. }
  152.  
  153.  
  154.  
  155. //
  156. // This is passed into the CreateDocumentWindow routine
  157. // to add the contents to the newly created list.
  158. //
  159. static OSErr AddListContents ( ListRef theList, void* refCon )
  160. {
  161.     AddToList ( theList, "\pApple Extras\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  162.     AddToList ( theList, "\pApplications\t-\tfolder\tWed, Dec 18, 1995, 12:04 pm" );
  163.     AddToList ( theList, "\pClean System Folder\t-\tfolder\tThu, Dec 7, 1995, 1:46 pm" );
  164.     AddToList ( theList, "\pCommunications\t-\tfolder\tWed, Dec 6, 1995, 2:21 pm" );
  165.     AddToList ( theList, "\pCopland Stuff\t-\tfolder\tTue, Aug 29, 1995, 1:14 pm" );
  166.     AddToList ( theList, "\pDevelopment\t-\tfolder\tWed, Dec 13, 1995, 4:27 pm" );
  167.     AddToList ( theList, "\pDocumentation\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  168.     AddToList ( theList, "\pDTS\t-\tfolder\tWed, Dec 18, 1995, 1:07 pm" );
  169.     AddToList ( theList, "\pMisc\t-\tfolder\tMon, Dec 16, 1995, 8:56 pm" );
  170.     AddToList ( theList, "\pProjects\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  171.     AddToList ( theList, "\pStress\t-\tfolder\tTue, Aug 29, 1995, 1:14 pm" );
  172.     AddToList ( theList, "\pSystem Folder\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  173.     AddToList ( theList, "\pSystem Installers\t-\tfolder\tWed, Dec 6, 1995, 2:21 pm" );
  174.     AddToList ( theList, "\pSystem Picker\t-\tapplication program\tMon, Dec 16, 1995, 8:56 pm" );
  175.     AddToList ( theList, "\pTools\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  176.     AddToList ( theList, "\pAppleScriptâ„¢ Utilities\t-\tfolder\tThu, Dec 7, 1995, 12:46 pm" );
  177.     AddToList ( theList, "\pDebuggers\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  178.     AddToList ( theList, "\pDebugging Modern Memory Manager\t-\tfolder\tMon, Dec 16, 1995, 8:56 pm" );
  179.     AddToList ( theList, "\pDebugging OD Part Editors\t33K\tSimpleText document\tThu, Dec 19, 1995, 12:07 pm" );
  180.     AddToList ( theList, "\pDocumentation Viewers\t-\tfolder\tMon, Dec 16, 1995, 12:56 pm" );
  181.     AddToList ( theList, "\pPartMaker 4.3\t-\tfolder\tMon, Dec 16, 1995, 8:56 pm" );
  182.     AddToList ( theList, "\pReference\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  183.     AddToList ( theList, "\pResource Editors\t5,049K\tfolder\tWed, Dec 6, 1995, 2:21 pm" );
  184.     AddToList ( theList, "\pSDK\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  185.     AddToList ( theList, "\pSoundMgr3.2Dev.sea\t198K\tapplication program\tThu, Dec 19, 1995, 12:07 pm" );
  186.     
  187.     
  188.     return noErr;
  189.     
  190. } // AddContents
  191.  
  192.  
  193.  
  194.  
  195. //
  196. // This is called to create the List Manager list, and initialize it.
  197. //
  198. static OSErr CreateContentList ( WindowRef theWindow, tContentsProcPtr contentsProc, void* refCon )
  199. {
  200.     OSErr        theErr;
  201.     ListHandle    theList;
  202.     Point        cellSize = {0, 0};
  203.     Cell        firstCell = {0, 0};
  204.     Rect         dataRect = {0, 0, 0, 1};
  205.     Rect        viewRect;
  206.     
  207.     
  208.     // The width of a cell needs to be 32767 so any hiliting
  209.     // doesn't stop short of the window width after it's been resized.
  210.     
  211.     viewRect = theWindow->portRect;
  212.     InsetRect ( &viewRect, 20, 20 );
  213.     viewRect.right -= 15;                // minus scrollbar width
  214.     theList = LNew ( &viewRect, &dataRect, cellSize, kLDEFID, theWindow, 
  215.                         false, false, false, true );
  216.  
  217.     #ifndef USE_LDEF
  218.         theErr = PatchListLDEF ( theList );
  219.         if ( theErr )
  220.             return theErr;
  221.     #endif
  222.     
  223.     if ( theList )
  224.     {
  225.         static short    theOffsets[] = { 4, 0, 100, 134, 200 };
  226.         
  227.         (*theList)->refCon = (long) &theOffsets;
  228.         (*theList)->selFlags = lOnlyOne;
  229.         SetWRefCon ( theWindow, (long) theList );
  230.         
  231.         // Since the calling routine is always in the same architecture type as
  232.         // the callback routine, we don't need to worry about any Mixed Mode
  233.         // complications. We just treat it as a straight forward routine pointer.
  234.         if ( contentsProc )
  235.         {
  236.             theErr = (*contentsProc) ( theList, refCon );
  237.             if ( theErr )
  238.                 return theErr;
  239.         }
  240.         
  241.         LSetSelect ( true, firstCell, theList );
  242.         
  243.         // Now the list has been fully prepared, turn the drawing mode on
  244.         LSetDrawingMode ( true, theList );
  245.     }
  246.     
  247.     return noErr;
  248.     
  249. } // CreateContentList
  250.  
  251.  
  252.  
  253. void DoActivate ( EventRecord* theEvent )
  254. {
  255.     Boolean        bActiveFlag = theEvent->modifiers & resumeFlag;
  256.     GrafPtr        savePort;
  257.     WindowRef    theWindow = (WindowRef) theEvent->message;
  258.     
  259.     
  260.     GetPort ( &savePort );
  261.     SetPortWindowPort ( theWindow );
  262.     LActivate ( bActiveFlag, (ListRef) GetWRefCon ( theWindow ) );
  263.     SetPort ( savePort );
  264.     
  265.     return;
  266. }
  267.  
  268.  
  269.  
  270. void DoUpdate ( EventRecord* theEvent )
  271. {
  272.     GrafPtr            savePort;
  273.     CGrafPtr        thePort;
  274.     WindowRef        theWindow = (WindowRef) theEvent->message;
  275.     ListRef            theList = nil;
  276.     Rect            theRect;
  277.     
  278.     
  279.     
  280.     thePort = GetWindowPort ( theWindow );
  281.     
  282.     GetPort ( &savePort );
  283.     SetPortWindowPort ( theWindow );
  284.     BeginUpdate ( theWindow );                    // visRgn temporarily = updateRgn
  285.     EraseRect ( &thePort->portRect );
  286.     
  287.     theList = (ListRef) GetWRefCon ( theWindow );
  288.     if ( theList )
  289.     {
  290.         PenNormal ( );
  291.         GetListRect ( theList, &theRect );
  292.         InsetRect ( &theRect, -1, -1 );
  293.         FrameRect ( &theRect );
  294.         
  295.         LUpdate ( thePort->visRgn, theList );
  296.     }
  297.     
  298.     EndUpdate ( theWindow );                    // restore normal visRgn of grafport
  299.     SetPort ( savePort );
  300.     
  301.     return;
  302. }
  303.  
  304.  
  305.  
  306. void DoContentClick ( WindowRef theWindow, EventRecord* theEvent )
  307. {
  308.     WindowRef    frontWindow;
  309.     
  310.     // If a movable modal is active, ignore click in an inactive 
  311.     // window, otherwise select it or handle the content click.
  312.     
  313.     frontWindow = FrontWindow ( );
  314.     if ( theWindow != frontWindow )
  315.     {
  316.         if ( IsMovableModal ( frontWindow ) )
  317.             SysBeep ( 30 );
  318.         else
  319.             SelectWindow ( theWindow );
  320.     }
  321.     else
  322.     {
  323.         GrafPtr        savePort;
  324.         
  325.         GetPort ( &savePort );
  326.         SetPortWindowPort ( theWindow );
  327.         HandleListClick ( theWindow, theEvent );
  328.         SetPort ( savePort );
  329.     }
  330.     
  331.     return;
  332.     
  333. } // DoContentClick
  334.  
  335.  
  336.  
  337. void DoDragWindow ( WindowRef theWindow, EventRecord* theEvent )
  338. {
  339.     WindowRef    frontWindow;
  340.     
  341.     
  342.     // If a movable modal is active, ignore click in an inactive 
  343.     // title bar, otherwise let the Window Manager handle it.
  344.     
  345.     frontWindow = FrontWindow ( );
  346.     if ( theWindow != frontWindow && IsMovableModal ( frontWindow ) )
  347.         SysBeep ( 30 );
  348.     else                                
  349.     {
  350.         RgnHandle    theRgn;
  351.         Rect        dragRect;
  352.         
  353.         theRgn = GetGrayRgn ( );
  354.         dragRect = (*theRgn)->rgnBBox;
  355.         DragWindow ( theWindow, theEvent->where, &dragRect );
  356.     }
  357.     
  358.     return;
  359. }
  360.  
  361.  
  362.  
  363.  
  364.  
  365.